﻿Imports System.Collections

Namespace DoFactory.GangOfFour.Interpreter.Structural

    ''' <summary>
    ''' MainApp startup class for Structural
    ''' Interpreter Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()
            Dim context As New Context()
            ' Usually a tree
            Dim list As New ArrayList()
            ' Populate 'abstract syntax tree'
            list.Add(New TerminalExpression())

            list.Add(New NonterminalExpression())

            list.Add(New TerminalExpression())

            list.Add(New TerminalExpression())

            ' Interpret
            For Each exp As AbstractExpression In list
                exp.Interpret(context)
            Next

            ' Wait for user
            'Console.ReadKey()

        End Sub

    End Class

    ''' <summary>
    ''' The 'Context' class
    ''' </summary>
    Class Context

    End Class

    ''' <summary>
    ''' The 'AbstractExpression' abstract class
    ''' </summary>
    MustInherit Class AbstractExpression
        Public MustOverride Sub Interpret(ByVal context As Context)
    End Class

    ''' <summary>
    ''' The 'TerminalExpression' class
    ''' </summary>
    Class TerminalExpression
        Inherits AbstractExpression

        Public Overrides Sub Interpret(ByVal context As Context)
            Console.WriteLine("Called Terminal.Interpret()")
        End Sub
    End Class

    ''' <summary>
    ''' The 'NonterminalExpression' class
    ''' </summary>
    Class NonterminalExpression
        Inherits AbstractExpression

        Public Overrides Sub Interpret(ByVal context As Context)
            Console.WriteLine("Called Nonterminal.Interpret()")
        End Sub
    End Class

End Namespace
